package com.senac.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.senac.model.Disciplina;
import com.senac.model.Professor;

public class DisciplinaDao {
	
	ProfessorDao profDao;
	
	
	public List<Disciplina> getDisciplinas() {		
		Connection con =  null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Disciplina disciplina = null;
		profDao = new ProfessorDao();
		List<Disciplina> list = new ArrayList<Disciplina>();
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("SELECT * FROM DISCIPLINA ORDER BY ID");
			rs = ps.executeQuery();
			while(rs.next()) {
				disciplina = new Disciplina();
				disciplina.setId(rs.getInt("ID"));
				disciplina.setNome(rs.getString("NOME"));
				disciplina.setCargaHoraria(rs.getInt("CARGA_HORARIA"));
				disciplina.setProfessor(getProfessorById(rs.getInt("ID_PROFESSOR")));
				disciplina.setConteudo(rs.getString("CONTEUDO").replaceAll("\\n", "<br/>"));
				list.add(disciplina);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			BDUtil.finalizeConnection(rs, ps, con);
		}				
		return list;
	}
	
	
	public List<Disciplina> getDisciplinasByProfessor(int idProfessor) {		
		Connection con =  null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Disciplina disciplina = null;
		profDao = new ProfessorDao();
		List<Disciplina> list = new ArrayList<Disciplina>();
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("SELECT * FROM DISCIPLINA WHERE ID_PROFESSOR = ?");
			ps.setInt(1, idProfessor);
			rs = ps.executeQuery();
			while(rs.next()) {
				disciplina = new Disciplina();
				disciplina.setId(rs.getInt("ID"));
				disciplina.setNome(rs.getString("NOME"));
				disciplina.setCargaHoraria(rs.getInt("CARGA_HORARIA"));
				disciplina.setProfessor(getProfessorById(rs.getInt("ID_PROFESSOR")));
				disciplina.setConteudo(rs.getString("CONTEUDO").replaceAll("\\n", "<br/>"));
				list.add(disciplina);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			BDUtil.finalizeConnection(rs, ps, con);
		}				
		return list;
	}

	public Disciplina getDisciplinasById(Integer id) {
		Connection con =  null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Disciplina disciplina = null;
		profDao = new ProfessorDao();
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("SELECT * FROM DISCIPLINA WHERE ID = ?");
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if(rs.next()) {
				disciplina = new Disciplina();
				disciplina.setId(rs.getInt("ID"));
				disciplina.setNome(rs.getString("NOME"));
				disciplina.setCargaHoraria(rs.getInt("CARGA_HORARIA"));
				disciplina.setProfessor(getProfessorById(rs.getInt("ID_PROFESSOR")));
				disciplina.setConteudo(rs.getString("CONTEUDO"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			BDUtil.finalizeConnection(rs, ps, con);
		}				
		return disciplina;
	}
	
	
	
	public boolean save(Disciplina disciplina) {
		Connection con =  null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("INSERT INTO DISCIPLINA (nome, carga_horaria, id_professor, conteudo) VALUES (?, ?, ?, ?)");			
			ps.setString(1, disciplina.getNome());
			ps.setInt(2, disciplina.getCargaHoraria());
			
			Professor professor = disciplina.getProfessor();
			if(professor != null && professor.getId() > 0){
				ps.setInt(3, professor.getId());
			}else{
				ps.setNull(3, Types.INTEGER);
			}
			
			ps.setString(4, disciplina.getConteudo());
			return (ps.executeUpdate() > 0);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}
	}

	
	
	
	public boolean update(Disciplina disciplina) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("UPDATE DISCIPLINA SET nome = ?, carga_horaria = ?, id_professor = ?, conteudo = ? WHERE ID = ?");
			ps.setString(1, disciplina.getNome());
			ps.setInt(2, disciplina.getCargaHoraria());
			
			Professor professor = disciplina.getProfessor();
			if(professor != null && professor.getId() > 0){
				ps.setInt(3, professor.getId());
			}else{
				ps.setNull(3, Types.INTEGER);
			}
			
			ps.setString(4, disciplina.getConteudo());
			ps.setInt(5, disciplina.getId());
			return ps.executeUpdate() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}

	}
	
	

	
	public boolean delete(Integer id) {
		Connection con =  null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("DELETE FROM DISCIPLINA WHERE ID = ?");			
			ps.setInt(1, id);
			return ps.executeUpdate() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}
	}
	
	
	private Professor getProfessorById(int id){
		Professor professor = profDao.getProfessorById(id);
		return professor;
	}
	
	
	
	
	
	
	
	

}
